import Antd, {
	message
} from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';

// export const rules = (function(){
// 	let isMobile = function(_rule,value){
// 		let pattern = /^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/;
// 		return pattern.test(value)
// 	}

// 	let isEmail = function(_rule,value){
// 		let pattern = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
// 		return pattern.test(value)
// 	}

// 	return {
// 		isMobile,
// 		isEmail
// 	}
// })()

const validator = function(_rule, value) {
	return value
}

export const _patternRules = {
	isAccount: {
		pattern: /^([1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8})|([A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+)$/,
		message: "账号登录只能用邮箱或手机",
		validator
	},
	isMobile: {
		pattern: /^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/,
		message: "手机号码格式不正确",
		validator
	},
	isEmail: {
		pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
		message: "电子邮箱格式正不确",
		validator
	},
	isPassword: {
		pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/,
		message: "长度至少为6，至少含有一个字母和一个数字",
		validator
	},
	isAffirmPassword: {
		pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/,
		message: "长度至少为6，至少含有一个字母和一个数字",
		validator
	}
}

export let isAccount = function(_rule, value) {
	let pattern = _patternRules.isAccount.pattern
	return pattern.test(value)
}
export let isMobile = function(_rule, value) {
	let pattern = _patternRules.isMobile.pattern
	return pattern.test(value)
}
export let isEmail = function(_rule, value) {
	let pattern = _patternRules.isMobile.pattern
	return pattern.test(value)
}
export let isPassword = function(_rule, value) {
	let pattern = _patternRules.isPassword.pattern
	return pattern.test(value)
}



export let account = [{
	required: true,
	pattern: _patternRules.isAccount.pattern,
	message: _patternRules.isAccount.message,
	trigger: ['change']
}]
export let accountEmail = [{
	required: true,
	pattern: _patternRules.isEmail.pattern,
	message: _patternRules.isEmail.message,
	trigger: ['change']
}]
export let password = [{
	required: true,
	pattern: _patternRules.isPassword.pattern,
	message: _patternRules.isPassword.message,
	trigger: ['change']
}]





export function rules(params = {}) {
	// let accountEmail = [{required:true,validator:async function(_rule,value){
	//          if(isEmail(_rule,value)) {
	//            return Promise.resolve()
	//          }else{
	//            return Promise.reject('邮箱帐号不合法')
	//          }
	//   },trigger:['change']}]

	let affirmPassword = [{
		required: true,
		validator: async (_rule, value) => {
			if (!params.password) return Promise.reject('密码匹配参数必须为 password');
			if (value === '') return Promise.reject('确认密码不能为空');
			if (value !== params.password) return Promise.reject('确认密码和设置密码不匹配');
			return Promise.resolve()
		}
	}]
	let emailCode = [{
		required: true,
		message: "邮箱验证码不能为空"
	}]



	return {
		account: accountEmail,
		password,
		affirmPassword,
		emailCode
	}
}




message.config({
	maxCount: 1
});

export function warning(msg, ...args) {
	// message.destroy();
	if (typeof msg === 'object') {
		message.warning(msg)
	} else {
		if (args.length == 1) {
			if (typeof args[0] === 'function')
				message.warning(msg, 1, args[0])
		} else {
			message.warning(msg, args[0], args[1])
		}
	}
}
export function success(msg, ...args) {
	// message.destroy();
	if (typeof msg === 'object') {
		message.success(msg)
	} else {
		if (args.length == 1) {
			if (typeof args[0] === 'function')
				message.success(msg, 1, args[0])
		} else {
			message.success(msg, args[0], args[1])
		}
	}
}
export function open(msg) {
	// message.destroy();
	message.open(msg)
}


/**
 * @desc 通用提示框
 */
export function hint(msg, type = 'open', ...args) {
	// message.destroy();
	if (typeof msg === 'object') {
		message[type](msg)
	} else {
		if (args.length == 1) {
			if (typeof args[0] === 'function')
				message[type](msg, 1, args[0])
		} else {
			message[type](msg, args[0], args[1])
		}
	}
}

export default Antd
